﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Fcdbas.Core.Reflection;

namespace Fcdbas.Core.Util
{
    public class ObjectHelper
    {
        /// <summary>
        /// 对象List转换成DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static System.Data.DataTable ObjectTODataTable<T>(List<T> objs)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            TypeX type = TypeX.Create(typeof(T));
            var piList = type.Type.GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
            foreach (System.Reflection.PropertyInfo pi in piList)
            {
                PropertyInfoX infoX = PropertyInfoX.Create(pi);
                if(infoX.Member.MemberType == System.Reflection.MemberTypes.Property)
                    dt.Columns.Add(infoX.Name, infoX.Type);
            }
            foreach (T t in objs)
            {
                DataRow dr = dt.NewRow();
                foreach (System.Reflection.PropertyInfo pi in piList)
                {
                    PropertyInfoX infoX = PropertyInfoX.Create(pi);
                    dr[infoX.Name] = infoX.GetValue(t);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        /// <summary>
        /// 对象List转换成DataTable
        /// watson 针对报表参数单独处理
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static System.Data.DataTable ObjectTODataTableParam<T>(List<T> objs)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            TypeX type = TypeX.Create(typeof(T));
            var piList = type.Type.GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
            foreach (System.Reflection.PropertyInfo pi in piList)
            {
                PropertyInfoX infoX = PropertyInfoX.Create(pi);
                if (infoX.Member.MemberType == System.Reflection.MemberTypes.Property)
                {
                    if ((infoX.Name.ToString().ToUpper() == "PARAMFIELD") || (infoX.Name.ToString().ToUpper() == "PARAMNAME") || (infoX.Name.ToString().ToUpper() == "PARAMLABEL") || (infoX.Name.ToString().ToUpper() == "PARAMREMARK") || (infoX.Name.ToString().ToUpper() == "PARAMVALUE"))
                    {
                        dt.Columns.Add(infoX.Name, infoX.Type);
                    }
                }
            }
            foreach (T t in objs)
            {
                DataRow dr = dt.NewRow();
                foreach (System.Reflection.PropertyInfo pi in piList)
                {
                    PropertyInfoX infoX = PropertyInfoX.Create(pi);
                    if ((infoX.Name.ToString().ToUpper() == "PARAMFIELD") || (infoX.Name.ToString().ToUpper() == "PARAMNAME") || (infoX.Name.ToString().ToUpper() == "PARAMLABEL") || (infoX.Name.ToString().ToUpper() == "PARAMREMARK") || (infoX.Name.ToString().ToUpper() == "PARAMVALUE"))
                    {
                        dr[infoX.Name] = infoX.GetValue(t);
                    }
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        public static object CloneObject(object obj)
        {
            using (MemoryStream buffer = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(buffer, obj);
                buffer.Position = 0;
                object temp = formatter.Deserialize(buffer);

                return temp;
            }
        }
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] SerialzieObject<T>(T obj)
        {
            using (MemoryStream m = new MemoryStream())
            {

                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(m, obj);

                return m.ToArray();

            }
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static T DeserialzieObject<T>(byte[] buffer)
        {
            using (MemoryStream m = new MemoryStream())
            {

                m.Write(buffer, 0, buffer.Length);
                m.Position = 0;

                BinaryFormatter bin = new BinaryFormatter();
                object obj = bin.Deserialize(m);
                if (obj == null)
                    return default(T);
                if (obj is T) return (T)obj;
                return (T)Convert.ChangeType(obj, typeof(T));
               

            }
        }
    }
}
